<?php

/**
 *      [Ʒ�ƿռ�] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: ads.inc.php 5708 2011-05-27 07:18:29Z menglingmin $
 */

if(!defined('IN_ADMIN')) {
	exit('Acess Denied');
}

$editvalue = array();
updatebrandadscache(false);
$editvalue = $_G['brandads'];
$editvalue['banner'] = htmlspecialchars_decode($editvalue['banner']);
$editvalue['sitetheme'] = $_G['setting']['sitetheme'];

$arr_allowed_var = array('enablehotgoods', 'enableconsume', 'enablegroupbuy', 'enablediscount', 'ads_show_type','banner', 'consume', 'discount', 'sidebarshop', 'sidebardiscountshop', 'sidebargoods', 'sidebarconsume',
	'hotgoods', 'hotshop', 'groupbuy', 'sidebargroupbuy','autonum');

if(!empty($_POST['valuesubmit'])) {
	$item = array();
	$key = $rpsql = $comma = '';
	if(!empty($_POST['newtopic'])) {
		$tkey = empty($_POST['topic']) ? 0 : count($_POST['topic']);
		foreach($_POST['newtopic'] as $key=>$value) {
			$_POST['topic'][$tkey]['image'] = $value['image'];
			$_POST['topic'][$tkey]['url'] = $_POST['newtopic'][$key+1]['url'];
			$tkey += 1;
			unset($_POST['newtopic'][$key+1]);
		}
	}
	

	foreach($_POST as $key=>$value) {

		if($_POST[$key]!=$editvalue[$key] || empty($_POST[$key])) {
			if(in_array($key, $arr_allowed_var)) {
				$rpsql .= "$comma ('$key', '$value') ";
				$comma = ', ';
			} elseif($key=='topic') {
				foreach($value as $k=>$v) {
					if($v['image'] == '' && $v['url'] == '') {
						unset($value[$k]);
					} else {
						$value[$k]['image']=trim(strip_tags($v['image']));
						$value[$k]['url']=trim(strip_tags($v['url']));
					}
					
				}
				if(count($value) > 7) {
						cpmsg('ads_topic_new_msg', '', 'error');
					}
				$value = addslashes(serialize($value));
				$rpsql .= "$comma ('$key', '$value') ";
				$comma = ', ';
			}

		}
	}
	if(!empty($rpsql)) {
		DB::query('REPLACE INTO '.DB::table('data').' (`variable`, `value`) VALUES '.$rpsql);
	}
	$sitetheme = saddslashes($_POST['sitetheme']);
	DB::query('REPLACE INTO '.DB::table('settings').' (`variable`, `value`) VALUES (\'sitetheme\', \''.$sitetheme.'\')');
	updatesettingcache();
	updatebrandadscache();//��ɻ���
	$_BCACHE->deltype('index');
	$_BCACHE->deltype('sidebar');
	cpmsg('message_success', 'admin.php?action=ads');

} else {

	$sitethemearr = array();
	$dir = opendir(B_ROOT.'./templates/site');
	while($entry = readdir($dir)) {
		if(strpos($entry, '.') === false) {
			$sitethemearr[] = array($entry, 'templates/site/'.$entry);
		}
	}
	shownav('global', 'ads_basic');
	showsubmenu('ads_basic');
	showtips('ads_tips');
	showformheader('ads');
	showhiddenfields(array('valuesubmit' => 'yes'));
	echo '<div style="border-top:1px dotted #DEEFFB;margin:5px 0 10px 0;padding:10px 0 0 5px;"><strong style="line-height:30px;">'.$lang['ads_sitetheme'].'</strong><br/>';
	showsetting('', array('sitetheme', $sitethemearr), $editvalue['sitetheme'], 'select');
	echo '&nbsp;&nbsp;&nbsp;&nbsp;'.$lang['ads_sitetheme_comment'].'</div>';
	echo '<div style="border-top:1px dotted #DEEFFB;margin:5px 0 10px 0;padding:10px 0 0 5px;"><strong style="line-height:30px;">'.$lang['ads_index_show_type'].'</strong><br/>
			<input name="ads_show_type" id="ads_show_type_topic" type="radio" style="border:none;" value="topic" onclick="show_ad_topic();" />'.lang('ads_topic').'
			<input checked name="ads_show_type" id="ads_show_type_banner" type="radio" style="border:none;" value="banner" onclick="show_ad_banner();" />'.lang('ads_banner').'
		</div>';
	showtableheader();
	echo '<tbody id="_ads_banner">';
	showsetting('ads_banner', 'banner', $editvalue['banner'], 'textarea');
	echo '</tbody>';
	echo '<tbody id="_ads_topic" style="display:none;"><tr><td class="td27" colspan="2">'.lang('ads_topic').'&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:100">'.lang('ads_topic_comment').'</td></tr>';
	echo '<tr><td style="border:none;"><table>';

	if(!empty($editvalue['topic'])) {

		foreach($editvalue['topic'] as $key=>$value) {
			echo '<tr class="noborder">
							<td class="vtop rowform">
								<input type="text" class="txt" value="'.$value['image'].'" name="topic['.$key.'][image]">
							</td>
							<td align="left" class="vtop rowform">
								<input type="text" class="txt" value="'.$value['url'].'" name="topic['.$key.'][url]">
							</td>
						</tr>';
		}

	}

	$remain_num = 7 - count($editvalue['topic']);
	echo <<<EOT
<script type="text/JavaScript">
	var rowtypedata = [
		[ 
			[1, '<input name="newtopic[][image]" value=""  type="text" class="txt">', 'vtop rowform'], 
			[1, '<input name="newtopic[][url]" value=""  type="text"  class="txt">', 'vtop rowform'],
		],
	];
	//js����cookie�ķ���
	function jSetCookie(key, value, lifetime) {
		var exp  = new Date();
		exp.setTime(exp.getTime() + lifetime*1000);
		document.cookie = key + "="+ escape (value) + ";expires=" + exp.toGMTString();
	}
	//jsȡ��cookie
	function jGetCookie(key) {
		var ckarr = document.cookie.match(new RegExp("(^| )"+key+"=([^;]*)(;|$)"));
		if(ckarr != null) return unescape(ckarr[2]); return null;
	}

	function showrow(obj){
		var remain_num =  jGetCookie('remainnum');
		if(remain_num == null) {
			jSetCookie('remainnum',  1, 600);
			addrow(obj, 0);
		} else {
			var newnum = Number(remain_num) + 1;
			if(remain_num < $remain_num ) {
				addrow(obj, 0);
				jSetCookie('remainnum', newnum, 600);
			} else if(remain_num == $remain_num) {
				alert("{$lang['ads_topic_new_alert']}");
				jSetCookie('remainnum', '', -3600);
			}
		}
	}
</script>
EOT;
	echo '<tr><td colspan="8"><div><a href="###" onclick="showrow(this)" class="addtr">'.$lang['ads_topic_new'].'</a></div></td></tr>';
	echo '</table></td></tr>';
	showtablefooter();

	
	showtableheader();
	showsetting('auto_num', array('autonum', array(
		array(1, lang('yes')),
		array(0, lang('no'))
	), true),$editvalue['autonum'], 'mradio');
	showsetting('ads_hotgoods', array('enablehotgoods', array(
		array(1, lang('yes'), array('hotgoodsext' => '')),
		array(0, lang('no'), array('hotgoodsext' => 'none'))
	), true), $editvalue['enablehotgoods'], 'mradio');
	showtagheader('tbody', 'hotgoodsext', $editvalue['enablehotgoods'], 'sub');
	showsetting('', 'hotgoods', $editvalue['hotgoods'], 'text', '', '', '', '');
	display_item("good", $editvalue['hotgoods'], 'hotgoods');
	showtagfooter('tbody');

	showsetting('ads_consume', array('enableconsume', array(
		array(1, lang('yes'), array('consumesext' => '')),
		array(0, lang('no'), array('consumeext' => 'none'))
	), true), $editvalue['enableconsume'], 'mradio');
	showtagheader('tbody', 'consumeext', $editvalue['enableconsume'], 'sub');
	showsetting('', 'consume', $editvalue['consume'], 'text');
	display_item("consume", $editvalue['consume'], 'consume');
	showtagfooter('tbody');


	showsetting('ads_groupbuy', array('enablegroupbuy', array(
		array(1, lang('yes'), array('groupbuyext' => '')),
		array(0, lang('no'), array('groupbuyext' => 'none'))
	), true), $editvalue['enablegroupbuy'], 'mradio');
	showtagheader('tbody', 'groupbuyext', $editvalue['enablegroupbuy'], 'sub');
	showsetting('', 'groupbuy', $editvalue['groupbuy'], 'text');
	display_item("groupbuy", $editvalue['groupbuy'], 'groupbuy');
	showtagfooter('tbody');


	showsetting('ads_discount', array('enablediscount', array(
		array(1, lang('yes'), array('discountext' => '')),
		array(0, lang('no'), array('discountext' => 'none'))
	), true), $editvalue['enablediscount'], 'mradio');
	showtagheader('tbody', 'discountext', $editvalue['enablediscount'], 'sub');
	showsetting('', 'discount', $editvalue['discount'], 'text');
	display_item("shop", $editvalue['discount'], 'discount');
	showtagfooter('tbody');


	showsetting('ads_hotshop', 'hotshop', $editvalue['hotshop'], 'text');
	display_item("shop", $editvalue['hotshop'], 'hotshop');

	showsetting('ads_sidebarshop', 'sidebarshop', $editvalue['sidebarshop'], 'text');
	display_item("shop", $editvalue['sidebarshop'], 'sidebarshop');

	showsetting('ads_sidebardiscountshop', 'sidebardiscountshop', $editvalue['sidebardiscountshop'], 'text');

	showsetting('ads_sidebargoods', 'sidebargoods', $editvalue['sidebargoods'], 'text');

	showsetting('ads_sidebarconsume', 'sidebarconsume', $editvalue['sidebarconsume'], 'text');
	display_item("consume", $editvalue['sidebarconsume'], 'sidebarconsume');

	showsetting('ads_sidebargroupbuy', 'sidebargroupbuy', $editvalue['sidebargroupbuy'], 'text');
	display_item("groupbuy", $editvalue['sidebargroupbuy'], 'sidebargroupbuy');

	showsubmit('settingsubmit', 'submit', '', $extbutton.(!empty($from) ? '<input type="hidden" name="from" value="'.$from.'">' : ''));
	showtablefooter();
	echo '
		<script tyle="text/javascript" charset="'.$_G['charset'].'">
		function show_ad_banner() {
			document.getElementById(\'_ads_banner\').style.display=\'\';
			document.getElementById(\'_ads_topic\').style.display=\'none\';
		}
		function show_ad_topic() {
			document.getElementById(\'_ads_banner\').style.display=\'none\';
			document.getElementById(\'_ads_topic\').style.display=\'\';
		}
			if("'.$editvalue['ads_show_type'].'"=="banner"){
					$("#ads_show_type_banner").attr("checked",true);
					show_ad_banner();
			}else{
					$("#ads_show_type_topic").attr("checked",true);
					show_ad_topic();
			}
		</script>';
	showformfooter();
	bind_ajax_form();
}

function display_item($mname, $itemids, $input_id) {
	$_itemids = explode(",", $itemids);
	foreach ($_itemids as $k => $v) {
		if (intval($v) > 0) {
			$itemids .= intval($v) . ",";
		}
	}
	if (strlen($itemids) > 1) {
		$itemids = preg_replace("/,$/", "", $itemids);
	} else {
		return;
	}
	$ret = "";
	$ret .= "<tr><td colspan=\"2\" class=\"td27\" style=\"border-top:none;\"><ul>";
	$query = DB::query("select * from " . DB::table($mname . "items") . " where itemid in (" . $itemids .")");
	while ($item = DB::fetch($query)) {
		$ret .= "<li style=\"padding:5px;color:#666;\">" . $item['subject'] . " <font color=\"#eee\" onMouseOver=\"this.style.color='red';\" onMouseOut=\"this.style.color='#eee';\" style=\"cursor:pointer;\" onclick=\"delete_item_in_inputtag(this, '" . $item['itemid'] . "', '" . $input_id . "');\">X</font></li>";
	}
	$ret .= "</ul></td></tr>";
	echo $ret;
}
?>